﻿using MediatR;
using PublicCommon;
using Newtonsoft.Json;
using T_Person.BaseRep.Interface;
using T_Person.Domain;
using T_Person.Read.APi.Applaction.Command;
using T_Person.Read.APi.Dto;

namespace T_Person.Read.APi.Applaction.CommandHandlers
{
    public class EmployeeSearchCommandHandler : IRequestHandler<EmployeeSearchCommand, PageResult<EmployeeDto>>
    {
        private readonly Cross_services_Helper cross_Services_Helper;
        private readonly IEmployeeRep employeeRep;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="employeeRep"></param>
        public EmployeeSearchCommandHandler(IEmployeeRep employeeRep, Cross_services_Helper cross_Services_Helper)
        {
            this.employeeRep = employeeRep;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<PageResult<EmployeeDto>> Handle(EmployeeSearchCommand request, CancellationToken cancellationToken)
        {
           
            var list = await employeeRep.GetvaluesByResult(x => x.IsDel.Equals(false));
            if (!string.IsNullOrEmpty(request.ename))
            {
                list = list.Where(x => x.Ename == request.ename);
            }
            if (!string.IsNullOrEmpty(request.rid))
            {
                list = list.Where(x => x.PostsId==request.rid);
            }
            if (!string.IsNullOrEmpty(request.did))
            {
                list = list.Where(x => x.DepartId == request.did);
            }
            if (!string.IsNullOrEmpty(request.phone))
            {
                list = list.Where(x => x.Ephone == request.phone);
            };
            if (!string.IsNullOrEmpty(request.style))
            {
                list = list.Where(x => x.EmployeeType== request.style);
            }
            var employeelist = from a in list
                               select new EmployeeDto
                               {
                                   Eaddress = a.Eaddress,
                                   Ebirthday = (!string.IsNullOrEmpty(a.Ebirthday.Value.ToString())) ? a.Ebirthday.Value.ToString("yyyy-MM-dd") : "暂无",
                                   EcardId = a.EcardId,
                                   Eid = a.Eid,
                                   Email = a.Email,
                                   Emarriage = a.Emarriage,
                                   EmployeeStyle = a.EmployeeStyle,
                                   EmployeeType = a.EmployeeType,
                                   Ename = a.Ename,
                                   EntrtDate = (!string.IsNullOrEmpty(a.EntrtDate.Value.ToString()))? a.EntrtDate.Value.ToString("yyyy-MM-dd"):"暂无",
                                   Eorigin = a.Eorigin,
                                   Ephone = a.Ephone,
                                   Eschool = a.Eschool,
                                   Esex = a.Esex,
                                   Especialized = a.Especialized,
                                   AuditState = a.AuditState,
                                   CreateBy = a.CreateBy,
                                   CreateDate = a.CreateDate,
                                   DepartId = a.DepartId,
                                   Dname = a.DepartId,
                                   IsDel = a.IsDel,
                                   PostsId = a.PostsId,
                                   Rname = a.PostsId,
                                   UpdateBy = a.UpdateBy,
                                   UpdateDate = a.UpdateDate,
                                   Eshangji=a.Eshangji,
                                   CreateById = a.CreateById,
                                   Egongling=(!string.IsNullOrEmpty(a.EntrtDate.ToString())&&a.EmployeeStyle>0)?(DateTime.Now.Year-a.EntrtDate.Value.Year):0
                               };
            int Listcount= employeelist.Count();
            int Pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pagesize);
            employeelist = employeelist.OrderByDescending(x => x.Eid).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize);
            return await Task.FromResult(new PageResult<EmployeeDto>
            {
                List = employeelist.ToList(),
                PageCount = Pagecount,
                TotalCount = Listcount
            });
        }
    }
}
